namespace Tools
{
    using System;
    using System.Text;
    
    public sealed class Converter
    {
        public static string ArrayToHexStr(byte[] Data)
        {
            return ArrayToHexStr(Data, 0, Data.Length);
        }
        
        public static string ArrayToHexStr(byte[] Data, int Offset, int Length)
        {
            if (Data == null)
            {
                throw new ArgumentNullException("Data", "Data Is Null.");
            }
            if (((Offset < 0) || (Length < 0)) || ((Offset + Length) > Data.Length))
            {
                throw new ArgumentOutOfRangeException();
            }
            StringBuilder builder = new StringBuilder();
            int num = Offset + Length;
            for (int i = Offset; i < num; i++)
            {
                builder.Append(string.Format("{0:X2}", Data[i]));
            }
            return builder.ToString();
        }
        
        public static string ArrayToHexStr(byte[] Data, int Offset, int Length, char Splitter)
        {
            if (Data == null)
            {
                throw new ArgumentNullException("Data", "Data Is Null.");
            }
            if (((Offset < 0) || (Length < 0)) || ((Offset + Length) > Data.Length))
            {
                throw new ArgumentOutOfRangeException();
            }
            StringBuilder builder = new StringBuilder();
            int num = Offset + Length;
            for (int i = Offset; i < num; i++)
            {
                builder.Append(string.Format("{0:X2}{1}", Data[i], Splitter));
            }
            if (builder.Length > 0)
            {
                builder.Remove(builder.Length - 1, 1);
            }
            return builder.ToString();
        }
        
        public static byte[] HexStrToArray(string HexStrData)
        {
            return HexStrToArray(HexStrData, 0, HexStrData.Length);
        }
        
        public static byte[] HexStrToArray(string HexStrData, int Offset, int Length)
        {
            if (HexStrData == null)
            {
                throw new ArgumentNullException("HexStrData", "HexStrData Is Null.");
            }
            if (((Offset < 0) || (Length < 0)) || ((Offset + Length) > HexStrData.Length))
            {
                throw new ArgumentOutOfRangeException();
            }
            byte[] buffer = new byte[Length / 2];
            try
            {
                for (int i = 0; i < buffer.Length; i++)
                {
                    buffer[i] = Convert.ToByte(HexStrData.Substring((i * 2) + Offset, 2), 0x10);
                }
            }
            catch
            {
                throw new ArithmeticException("HexStrData is not a legal hex string.");
            }
            return buffer;
        }
    }
}
